package participation.Impl;

import participation.dao.YzxTypeDao;
import participation.domain.YzxParticipant;
import participation.domain.YzxType;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class YzxTypeDaoImpl implements YzxTypeDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;

    @Override
    public void insert(YzxType yzxType) {
        String sql = "insert into yzx_type\n" +
                "        (type_id,type_name) \n" +
                "values(?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,yzxType.getType_id());
            psmt.setString(2,yzxType.getType_name());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long type_id) {
        String sql = "delete from yzx_type where type_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,type_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(YzxType yzxType) {
        String sql = "update yzx_type set";
        List<Object> param = new ArrayList<>();
        if (yzxType == null) {
            return;
        } else {
            if (yzxType.getType_id() != null) {
                sql += " type_id = ?,";
                param.add(yzxType.getType_id());
            }
            if (yzxType.getType_name() != null) {
                sql += " type_name = ?,";
                param.add(yzxType.getType_name());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where type_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,yzxType.getType_id());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public YzxType selectById(Long type_id) {
        String sql = "select * from yzx_type where type_id = ?";
        YzxType yzxType = new YzxType();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,type_id);
            rs = psmt.executeQuery();
            while (rs.next()){
                yzxType.setType_id(rs.getLong(1));
                yzxType.setType_name(rs.getString(2));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return yzxType;
    }

    @Override
    public List<YzxType> selectList(YzxType yzxType) {
        String sql = "select * from yzx_type where 1=1";
        List<YzxType> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (yzxType == null) {
            sql = "select * from yzx_type";
        } else {
            if(yzxType.getType_id() != null) {
                sql += " and type_id = ?";
                param.add(yzxType.getType_id());
            }
            if(yzxType.getType_name() != null) {
                sql += " and type_name = ?";
                param.add(yzxType.getType_name());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                YzxType yzxType1 = new YzxType();
                yzxType1.setType_id(rs.getLong(1));
                yzxType1.setType_name(rs.getString(2));
                list.add(yzxType1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }

}
